<template>
  <el-config-provider :size="getGlobalComponentSize" :locale="getGlobalI18n">
    <LockScreen v-if="themeConfig.isLockScreen" />
    <Setings ref="settingsRef" v-show="setLockScreen" />
    <CloseFull v-if="!themeConfig.isLockScreen" />
    <router-view v-if="setLockScreen" />
  </el-config-provider>
</template>

<script setup lang="ts" name="app">
import {
  defineAsyncComponent,
  computed,
  ref,
  onBeforeMount,
  onMounted,
  onUnmounted,
  nextTick,
  watch
} from 'vue'
import { useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { storeToRefs } from 'pinia'
import { useTagsViewRoutes } from '@/stores/tagsViewRoutes'
import { useThemeConfig } from '@/stores/themeConfig'
import other from '@/utils/other'
import { Local, Session } from '@/utils/storage'
import mittBus from '@/utils/mitt'
import setIntroduction from '@/utils/setIconfont'

// 引入组件
const LockScreen = defineAsyncComponent(() => import('@/layout/lockScreen/index.vue'))
const Setings = defineAsyncComponent(() => import('@/layout/navBars/topBar/SystemSettings.vue'))
const CloseFull = defineAsyncComponent(() => import('@/layout/navBars/topBar/closeFull.vue'))

// 定义变量内容
const { messages, locale } = useI18n()
const settingsRef = ref()
const route = useRoute()
const stores = useTagsViewRoutes()
const storesThemeConfig = useThemeConfig()
const { themeConfig } = storeToRefs(storesThemeConfig)

// 设置锁屏时组件显示隐藏
const setLockScreen = computed(() => {
  // 防止锁屏后，刷新出现不相关界面
  return themeConfig.value.isLockScreen
    ? themeConfig.value.lockScreenTime > 1
    : themeConfig.value.lockScreenTime >= 0
})
// // 获取版本号
// const getVersion = computed(() => {
// 	let isVersion = false
// 	if (route.path !== '/login') {
//     const version = Local.get('version')
// 		// @ts-ignore
// 		if ((version && version !== __NEXT_VERSION__) || !version) isVersion = true
// 	}
// 	return isVersion
// })
// 获取全局组件大小
const getGlobalComponentSize = computed(() => {
  return other.globalComponentSize()
})
// 获取全局 i18n
const getGlobalI18n = computed(() => {
  return messages.value[locale.value]
})
// 设置初始化，防止刷新时恢复默认
onBeforeMount(() => {
  // 设置批量第三方 icon 图标
  setIntroduction.cssCdn()
  // 设置批量第三方 js
  setIntroduction.jsCdn()
})
// 页面加载时
onMounted(() => {
  nextTick(() => {
    // 监听布局配'置弹窗点击打开
    mittBus.on('openSettingsDrawer', () => {
      settingsRef.value.openDrawer()
    })
    // 获取缓存中的布局配置
    const configStyle = Local.get('themeConfigStyle')
    if (configStyle) document.documentElement.style.cssText = configStyle
    // 获取缓存中的全屏配置
    const isTagViewCurrenFull = Session.get('isTagViewCurrenFull')
    if (isTagViewCurrenFull) stores.setCurrenFullscreen(isTagViewCurrenFull)
  })
})
// 页面销毁时，关闭监听布局配置/i18n监听
onUnmounted(() => {
  mittBus.off('openSettingsDrawer', () => {})
})
// 监听路由的变化，设置网站标题
watch(
  () => route.path,
  () => {
    other.useTitle()
  },
  {
    deep: true
  }
)
</script>
